package net.wanho.controller;

import net.wanho.entity.User;
import net.wanho.service.UserService;
import net.wanho.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Author：汤小洋
 * Date：2025-06-13 16:38
 * Description：<描述>
 */
@WebServlet("/register")
public class RegisterController extends HttpServlet {

    private UserService userService = new UserServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("name");
        String pwd = req.getParameter("pwd");
        Integer age = Integer.parseInt(req.getParameter("age"));
        String sex = req.getParameter("sex");
        Double height = Double.parseDouble(req.getParameter("height"));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date birthday;
        try {
             birthday = sdf.parse(req.getParameter("birthday"));
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }

        User user = new User(null, name, pwd, age, sex, height, birthday);
        userService.register(user);

        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();
        out.print("注册成功!");
        out.close();
    }
}
